package Add;

import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

@WebServlet("/AddHeroServlet")
public class AddHeroServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Map<String, String[]> parameterMap = req.getParameterMap();
            Hero hero = new Hero();
            BeanUtils.populate(hero,parameterMap);
            ArrayList<Hero> hero1 = (ArrayList<Hero>) getServletContext().getAttribute("HERO");
            hero1.add(hero);
            getServletContext().setAttribute("HERO",hero1);
            resp.sendRedirect(req.getContextPath()+"/index.jsp");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
